package com.rose.springbootgeneratefile.codegen.mybatis3;

import com.rose.springbootgeneratefile.api.GeneratedJavaFile;
import com.rose.springbootgeneratefile.api.GeneratedXmlFile;
import com.rose.springbootgeneratefile.api.IntrospectedTable;
import com.rose.springbootgeneratefile.api.PropertyRegistry;
import com.rose.springbootgeneratefile.api.dom.java.CompilationUnit;
import com.rose.springbootgeneratefile.codegen.AbstractGenerator;
import com.rose.springbootgeneratefile.codegen.AbstractJavaGenerator;
import com.rose.springbootgeneratefile.codegen.mybatis3.model.BaseRecordGenerator;

import java.util.ArrayList;
import java.util.List;

public class IntrospectedTableMyBatis3Impl extends IntrospectedTable {
    /** The java model generators. */
    protected List<AbstractJavaGenerator> javaModelGenerators;

    public IntrospectedTableMyBatis3Impl() {
        super(TargetRuntime.MYBATIS3);
        javaModelGenerators = new ArrayList<AbstractJavaGenerator>();
    }

    protected void calculateJavaModelGenerators() {
//        if (getRules().generateExampleClass()) {
//            AbstractJavaGenerator javaGenerator = new ExampleGenerator();
//            initializeAbstractGenerator(javaGenerator, warnings,
//                    progressCallback);
//            javaModelGenerators.add(javaGenerator);
//        }
//
//        if (getRules().generatePrimaryKeyClass()) {
//            AbstractJavaGenerator javaGenerator = new PrimaryKeyGenerator();
//            initializeAbstractGenerator(javaGenerator, warnings,
//                    progressCallback);
//            javaModelGenerators.add(javaGenerator);
//        }

//        if (getRules().generateBaseRecordClass()) {
            AbstractJavaGenerator javaGenerator = new BaseRecordGenerator();
            initializeAbstractGenerator(javaGenerator);
            javaModelGenerators.add(javaGenerator);
//        }

//        if (getRules().generateRecordWithBLOBsClass()) {
//            AbstractJavaGenerator javaGenerator = new RecordWithBLOBsGenerator();
//            initializeAbstractGenerator(javaGenerator, warnings,
//                    progressCallback);
//            javaModelGenerators.add(javaGenerator);
//        }
    }
    @Override
    public void calculateGenerators() {
//        calculateJavaModelGenerators(warnings, progressCallback);
//        calculateDAOGenerators(warnings, progressCallback);
//        calculateSqlMapGenerator(warnings, progressCallback);
        calculateJavaModelGenerators();
    }
    @Override
    public List<GeneratedJavaFile> getGeneratedJavaFiles() {
        List<GeneratedJavaFile> answer = new ArrayList<GeneratedJavaFile>();

        for (AbstractJavaGenerator javaGenerator : javaModelGenerators) {
            List<CompilationUnit> compilationUnits = javaGenerator
                    .getCompilationUnits();
            for (CompilationUnit compilationUnit : compilationUnits) {
                GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
                        context.getJavaModelGeneratorConfiguration()
                                .getTargetProject(),
                        context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
                        context.getJavaFormatter());
                answer.add(gjf);
            }
        }

//        for (AbstractJavaGenerator javaGenerator : clientGenerators) {
//            List<CompilationUnit> compilationUnits = javaGenerator
//                    .getCompilationUnits();
//            for (CompilationUnit compilationUnit : compilationUnits) {
//                GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
//                        context.getJavaClientGeneratorConfiguration()
//                                .getTargetProject(),
//                        context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
//                        context.getJavaFormatter());
//                answer.add(gjf);
//            }
//        }

        return answer;
    }

    protected void initializeAbstractGenerator(AbstractGenerator abstractGenerator) {
        if (abstractGenerator == null) {
            return;
        }
        abstractGenerator.setContext(context);
        abstractGenerator.setIntrospectedTable(this);
//        abstractGenerator.setProgressCallback(progressCallback);
//        abstractGenerator.setWarnings(warnings);
    }
    @Override
    public List<GeneratedXmlFile> getGeneratedXmlFiles() {
        return null;
    }
}
